package im

import (
    "time"
	"dhfshop/domain/im"
	"dhfshop/models/im"
	"dhfshop/models/isv"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/validation"
)

type TemplateReqController struct {
	base.BaseController
}
      
func (this *TemplateReqController) Post() {
	v := struct {
	    ToUser  string   `form:"to_user" valid:"Required"`
		Xid     string   `form:"xid" valid:"Required"`     //formid_cache业务单据号
		Name    string   `form:"name" valid:"Required"`    //名称
		Remark  string   `form:"remark" valid:"Required"`  //备注
		Tips    string   `form:"tips" valid:"Required"`    //温馨提示
	}{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return	
	}
	//partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	//curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	sellerAppID := beego.AppConfig.String("SellerAppID")
	adminAuthorize, err := isv.GetAuthorizeByAppId(sellerAppID)
	if err != nil {
		this.ServeError("GetAuthorizeByAppId:"+sellerAppID+err.Error())
		return
	}
	templateId := "D01xI2ZIH5JsRrdZ5zrIavNM7nJRjze0biGdWVyS_9Y"
	page := "pages/distribution/add/index"
	formId, err := im.GetFormIdByOpenId(v.ToUser,"audit", v.Xid)
	if err != nil {
		this.ServeError("GetFormIdByOpenId:" + err.Error())
		return
	}
	err = imlayer.AuditResultTempMsg(adminAuthorize.AccessToken,page,v.ToUser,
	                                 templateId,formId,v.Name,v.Remark,v.Tips)
	if err != nil {
	    this.ServeError("AuditResultTempMsg:" + err.Error())
		return
	}
	rsp := struct {
		Xid       string     `json:"xid"`
	    Created   time.Time  `json:"created"`
	}{Xid: v.Xid, Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}

func (this *TemplateReqController) PostCommon() {
	v := struct {
	    AppId      string   `form:"app_id" valid:"Required"`
		Page       string   `form:"page"`  
		ToUser     string   `form:"to_user" valid:"Required"`
		TemplateId string   `form:"template_id" valid:"Required"` 
		FormId     string   `form:"form_id" valid:"Required"`
		Value1     string   `form:"value1"`
		Value2     string   `form:"value2"`
		Value3     string   `form:"value3"`
		Value4     string   `form:"value4"`
	}{}
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return	
	}
	//partnerId := int64(this.Ctx.Input.GetData("curCompanyId").(int))
	//curUserId := int64(this.Ctx.Input.GetData("curUserId").(int))
	custAccount, err := isv.GetCustAccountByAppId(v.AppId)  
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	err = imlayer.CommonTempMsgSend(custAccount.AccessToken,v.Page,v.ToUser,
	                                 v.TemplateId,v.FormId,v.Value1,v.Value2,v.Value3,v.Value4)
	if err != nil {
	    this.ServeError("CommonTempMsgSend:" + err.Error())
		return
	}
	rsp := struct {
		Result    string     `json:"result"`
	    Created   time.Time  `json:"created"`
	}{Result: "success", Created:time.Now().UTC()}
	this.Data["json"] = rsp
	this.ServeJSON()
}
